/**
 * 
 */
package it.irondev.appturismo.action;

import it.irondev.appturismo.entity.Difficulty;
import it.irondev.appturismo.entity.Gallery;
import it.irondev.appturismo.entity.Hike;
import it.irondev.appturismo.entity.Language;
import it.irondev.appturismo.filter.HikeFilter;
import it.irondev.appturismo.service.IGalleryService;
import it.irondev.appturismo.service.IHikeService;
import it.irondev.appturismo.service.ILanguageService;
import it.irondev.appturismo.util.ActionHelper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * @author federicocerruto
 *
 */

@Controller("hikeAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class HikeAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4967349210061989357L;

	@Autowired
	IHikeService serviceHike;

	@Autowired
	ILanguageService serviceLanguage;

	
	@Autowired
	IGalleryService galleryService;

	private Integer idGallery;
	private List<Language> listLanguage;
	private List<Difficulty> listDifficulties;
	private Hike entityBean;
	private Long idHike;
	private List<Hike> hikes;
	private String stringaSearchLanguage;

	
	public String list() {
		listLanguage = serviceLanguage.getAllLanguages();
		stringaSearchLanguage = ActionHelper.buildStringSearch(listLanguage); 
		return "listHikes";
	}

	
	public String listJson() {
		hikes = serviceHike.getAllHikes();
//		listDifficulties = serviceHike.getAllDifficulties();
		return "listHikeJson";
	}

	
	public String nuovo() {
		listLanguage = serviceLanguage.getAllLanguages();
		listDifficulties = serviceHike.getAllDifficulties();
		return "newHike";
	}

	
	public String edit() {
		listLanguage = serviceLanguage.getAllLanguages();
		HikeFilter filter = new HikeFilter();
		filter.setId(idHike.intValue());
		List<Hike> tmpHikes = serviceHike.getHikeByFilter(filter);
		if(tmpHikes.size()>0)
			entityBean = tmpHikes.get(0);
		listDifficulties = serviceHike.getAllDifficulties();
		return "editHike";
	}

	public void delete() {
		if (getOper().equalsIgnoreCase("del")) {
			int idEntity = Integer.parseInt(getId());
			serviceHike.deleteHike(idEntity);
		}
	}

	public String update() {
		serviceHike.updateHike(entityBean);
		return "updateHike";
	}

	public String add() {
		serviceHike.addHike(entityBean);
		return "addedHike";
	}

	public String addGallery() {
		Gallery tmpGallery = new Gallery();
		galleryService.addGallery(tmpGallery);
		idGallery = tmpGallery.getIdgallery();
		HikeFilter filter = new HikeFilter();
		filter.setId(idHike.intValue());
		entityBean = serviceHike.getHikeByFilter(filter).get(0);
		entityBean.setGallery(galleryService.getGalleryById(idGallery));
		serviceHike.updateHike(entityBean);
		return "addHikeGallery";
	}
	

	/* 
	 * START MOBILE ACTION
	 */



	public String listMobile() {
		HikeFilter filter = new HikeFilter();
		String ln = getLocale().getLanguage();
		filter.setIdlanguage(serviceLanguage.getLanguageByPrefix(ln).getIdlanguage());
		hikes = serviceHike.getHikeByFilter(filter);
		return "listHikesMobile";
	}
	
	public String detailMobile() {
		HikeFilter filter = new HikeFilter();
		filter.setId(idHike.intValue());
		List<Hike> tmpHikes = serviceHike.getHikeByFilter(filter);
		if(tmpHikes.size()>0)
			entityBean = tmpHikes.get(0);
		return "detailHikeMobile";
	}
	
	
	
	
	public List<Language> getListLanguage() {
		return listLanguage;
	}


	public void setListLanguage(List<Language> listLanguage) {
		this.listLanguage = listLanguage;
	}


	public Hike getEntityBean() {
		return entityBean;
	}


	public void setEntityBean(Hike entityBean) {
		this.entityBean = entityBean;
	}


	public Long getIdHike() {
		return idHike;
	}


	public void setIdHike(Long idHike) {
		this.idHike = idHike;
	}


	public List<Hike> getHikes() {
		return hikes;
	}


	public void setHikes(List<Hike> hikes) {
		this.hikes = hikes;
	}


	public String getStringaSearchLanguage() {
		return stringaSearchLanguage;
	}


	public void setStringaSearchLanguage(String stringaSearchLanguage) {
		this.stringaSearchLanguage = stringaSearchLanguage;
	}


	public List<Difficulty> getListDifficulties() {
		return listDifficulties;
	}


	public void setListDifficulties(List<Difficulty> listDifficulties) {
		this.listDifficulties = listDifficulties;
	}

	
	/**
	 * @return the idGallery
	 */
	public Integer getIdGallery() {
		return idGallery;
	}


	/**
	 * @param idGallery the idGallery to set
	 */
	public void setIdGallery(Integer idGallery) {
		this.idGallery = idGallery;
	}
}
